package cn.net.yato.picture.mapper;

import cn.net.yato.picture.entity.Permission;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName PermissionMapper
 * @Description TODO
 * @Author yato
 * @Date 2020/2/17 14:41
 * @Version 1.0
 */
@Component
public interface PermissionMapper extends BaseMapper<Permission> {

    /**
     * 查询所有权限信息
     * @return
     */
    @Select("SELECT  permission.id,permission.permission_id,permission.`name`," +
            "permission.description,permission.url,permission.perms,permission.parent_id," +
            "permission.type,permission.icon,permission.`status`,permission.create_time," +
            "permission.update_time  FROM  permission")
    @Results(id = "permissionResult" ,value = {
            @Result(property = "id", column = "id"),
            @Result(property = "permissionId", column = "permission_id"),
            @Result(property = "name", column = "name")
    })
    List<Permission> getAllPermissions();

    /**
     * 根据用户ID查询所拥有的权限信息
     * @param userId
     * @return
     */
    @Select("SELECT  permission.id,permission.permission_id,permission.`name`," +
            "permission.description,permission.url,permission.perms,permission.parent_id," +
            "permission.type,permission.icon,permission.`status`,permission.create_time," +
            "permission.update_time FROM  permission ,user_permission WHERE " +
            "permission.permission_id = user_permission.permission_id  AND " +
            "user_permission.user_id = #{userId}")
    @ResultMap(value = "permissionResult")
    List<Permission> getPermissionsByUserId(@Param(value = "userId") String userId);

    /**
     * 根据角色ID查询所拥有的权限信息
     * @param roleId
     * @return
     */
    @Select("SELECT  permission.id,permission.permission_id,permission.`name`," +
            "permission.description,permission.url,permission.perms,permission.parent_id," +
            "permission.type,permission.icon,permission.`status`,permission.create_time," +
            "permission.update_time  FROM  role_permission ,permission  WHERE " +
            "role_permission.permission_id = permission.permission_id  AND " +
            "role_permission.role_id = #{roleId}")
    @ResultMap(value = "permissionResult")
    List<Permission> getPermissionsByRoleId(@Param(value = "roleId") String roleId);
}
